package com.schedualnew.mq.util;

import cn.hutool.json.JSONUtil;
import com.schedualnew.mq.controller.MQApiController;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;

import java.io.IOException;

public class RequestClientUtil {

    private Logger logger = LogManager.getLogger(RequestClientUtil.class);

    public void sendSoapRequest( String param, String action, String url) {
        String result = "";
        //响应前端实体
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            StringEntity requestEntity = new StringEntity(param);
            httpPost.setEntity(requestEntity);
            httpPost.addHeader("SoapAction", action);
            try {
                CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
                result = EntityUtils.toString(httpResponse.getEntity(), "gbk");
                httpResponse.close();

                byte[] iso8859_1_bytes = result.getBytes("ISO-8859-1");
                String utf8Reulst = new String(iso8859_1_bytes, "UTF-8");
                logger.info("<<<<申请单同步>>>>：utf8Reulst"+ utf8Reulst);
            } catch (Exception e) {
                //如果请求失败，对日志的异常、成功状态字段赋值
                logger.info("互联互通webservice调用出错:" +e.toString());

            } finally {
                try {
                    httpClient.close();
                } catch (IOException e) {
                }
            }
        } catch (Exception ex) {

        }
    }
}
